﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Web.Security;

public partial class HistoryOrder : System.Web.UI.Page
{
    ShopDataContext dc = new ShopDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.User.Identity.IsAuthenticated)
        {
            //Response.Redirect("Login.aspx");
            FormsAuthentication.RedirectToLoginPage();
        }
        else
        {
            if (Request.QueryString["oId"] != null)
            {
                string orderId = Request.QueryString["oId"];
                int oId = int.Parse(orderId);

                var query = from od in dc.OrderDetails
                            join p in dc.Products on od.pId equals p.pId
                            where od.oId == oId
                            select p;

                var query2 = from od in dc.OrderDetails
                             where od.oId == oId
                             group od by od.oId into g
                             select g.Sum(od => od.oPrice * od.oQuantity);
                foreach (var item in query2)
                {
                    Label1.Text = "Sum :" + item.Value;
                    break;
                }
                Order o1 = dc.Orders.FirstOrDefault(o => o.oId == oId);
                int status = Convert.ToInt32(o1.oStatus);
                if (status != 0)
                {
                    btnCancel.Visible = false;
                }
                ListView1.DataSource = query;
                ListView1.DataBind();
            }
            else
            {
                Response.Redirect("ListHistoryOrders.aspx");
            }
        }
    }
    protected void ListView2_DataBound(object sender, EventArgs e)
    {

    }
    protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        //if (e.Item.ItemType == ListViewItemType.DataItem)
        //{
        //    Label lblStatus = e.Item.FindControl("oStatusLabel") as Label;
        //    Label lblTypePayment = e.Item.FindControl("oTypePaymentLabel") as Label;
        //    switch (lblStatus.Text)
        //    {
        //        case "0":
        //            lblStatus.Text = "Pending";
        //            break;
        //        case "1":
        //            lblStatus.Text = "Completed";
        //            break;
        //        case "2":
        //            lblStatus.Text = "Cancel";
        //            break;
        //    }
        //    switch (lblTypePayment.Text)
        //    {
        //        case "2":
        //            lblTypePayment.Text = "Credit Card";
        //            break;
        //        case "3":
        //            lblTypePayment.Text = "Direct Payments";
        //            break;
        //    }
        //}

    }
    protected void btnView_Click(object sender, EventArgs e)
    {

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
         if (Request.QueryString["oId"] != null)
         {
             string orderId = Request.QueryString["oId"];
             Order o1 = dc.Orders.FirstOrDefault(o => o.oId == Convert.ToInt32(orderId));
             o1.oStatus = 2;
             dc.SubmitChanges();
             Response.Redirect("ListHistoryOrders.aspx");
         }
    }
}